<?php
namespace app\admin\model;
use think\Model;
use location;
class Login extends Model
{
	//后台管理员用户登录
	public function index(){
		$data['user_name'] = trim(input('post.user_name'));
		$data['password'] = trim(md5(md5(input('post.password'))));
		$admin = db('admin')->field('admin_id,user_name,password,loginip,ipadress,logintime,loginnum,session_id')->where($data)->find();
		if($admin){
			session('admin_id',$admin['admin_id']);
			session('user_name',$admin['user_name']);
	    	session('loginip',$admin['loginip']);
	    	session('ipadress',$admin['ipadress']);
	    	session('logintime',$admin['logintime']);
	    	$arr['loginip'] = request()->ip();
			$location = new location\IpLocation();
    		$ips = $location->getlocation($arr['loginip']);
			$arr['ipadress'] = $ips['country'];
			$arr['logintime'] = date('Y-m-d H:i:s');
			$arr['loginnum'] = $admin['loginnum']+1;
			$arr['session_id'] = session_id();
			$datas = db('admin')->where('user_name',$data['user_name'])->setField($arr);
			if($datas){
                return 1;
			}else{
                return 3;
			}     
		}else{
		   //用户名或者密码错误
		   return 605;
		}
	}
}